package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import net.qhzw.vip.qiyu.entity.Area;
import net.qhzw.vip.qiyu.entity.DeliveryMan;
import net.qhzw.vip.qiyu.entity.Transport;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.AreaService;
import net.qhzw.vip.qiyu.service.DeliveryManService;
import net.qhzw.vip.qiyu.service.TransportService;
import net.qhzw.vip.qiyu.util.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("shop/transport")
public class TransportController {


    @Autowired
    private TransportService transportService;

    @Autowired
    private DeliveryManService deliveryManService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public JsonResult<IPage<Transport>> page(Transport transport, PageParam<Transport> page) {
        IPage<Transport> transports = transportService.page(page,
                new LambdaQueryWrapper<Transport>()
                        .like(StringUtils.isNotBlank(transport.getTransName()), Transport::getTransName, transport.getTransName()));
        return JsonResult.ok(transports);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    public JsonResult<Transport> info(@PathVariable("id") Long id) {
        Transport transport = transportService.getTransportAndAllItems(id);
        return JsonResult.ok(transport);
    }

    /**
     * 保存
     */
    @PostMapping
    public JsonResult save(@RequestBody Transport transport) {
        Date createTime = new Date();
        transport.setCreateTime(createTime);
        DeliveryMan deliveryMan = deliveryManService.getByAreaName(transport.getTransName());
        if (deliveryMan == null) {
            JsonResult.fail("输入有误! " + transport.getTransName() + " 自提点不存在");
        }
        if (deliveryMan != null) {
            transport.setDeliveryId(deliveryMan.getId());
        }
        transportService.insertTransportAndTransfee(transport);
        return JsonResult.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public JsonResult update(@RequestBody Transport transport) {
        DeliveryMan deliveryMan = deliveryManService.getByAreaName(transport.getTransName());
        if (deliveryMan == null) {
            JsonResult.fail("输入有误! " + transport.getTransName() + " 自提点不存在");
        }
        transport.setDeliveryId(deliveryMan.getId());
        transportService.updateTransportAndTransfee(transport);
        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public JsonResult delete(@RequestBody Long[] ids) {
        transportService.deleteTransportAndTransfeeAndTranscity(ids);
        // 删除运费模板的缓存
        for (Long id : ids) {
            transportService.removeTransportAndAllItemsCache(id);
        }
        return JsonResult.ok();
    }


    /**
     * 获取运费模板列表
     */
    @GetMapping("/list")
    public JsonResult<List<Transport>> list() {
        List<Transport> list = transportService.list();
        return JsonResult.ok(list);
    }


}
